﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using seo.Data;
using MySql.Data.MySqlClient;

namespace seo.DAO
{
    public class WebsiteDao
    {
        private MySqlConnection conn;

        public WebsiteDao(MySqlConnection conn)
        {
            this.conn = conn;
        }

        public List<Website> getAllWebsite() {

            List<Website> list = new List<Website>();

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "select id,domainname,exp_group,exp_pcnt,imp_value,belong_group,exp_home_pcnt,minlink,maxlink from web_site where deleteflg='0'";

            reader = command.ExecuteReader();
            
            while (reader.Read()) {
                Website site = new Website();

                site.id = reader.GetInt32(0);
                site.domainname = reader.GetString(1);
                site.exp_group = reader.GetString(2);
                site.exp_pcnt = reader.GetInt32(3);
                site.imp_value = reader.GetInt32(4);
                site.belong_group = reader.GetString(5);
                site.exp_home_pcnt = reader.GetInt32(6);
                site.minlink = reader.GetInt32(7);
                site.maxlink = reader.GetInt32(8);
                
                list.Add(site);
            }

            return list;
        }

        public Website getWebsiteById(int id)
        {

            Website site = null;

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "select id,domainname,exp_group,exp_pcnt,imp_value,belong_group,exp_home_pcnt,minlink,maxlink from web_site where deleteflg='0' and id = " + id;

            reader = command.ExecuteReader();

            while (reader.Read())
            {
                site = new Website();

                site.id = reader.GetInt32(0);
                site.domainname = reader.GetString(1);
                site.exp_group = reader.GetString(2);
                site.exp_pcnt = reader.GetInt32(3);
                site.imp_value = reader.GetInt32(4);
                site.belong_group = reader.GetString(5);
                site.exp_home_pcnt = reader.GetInt32(6);
                site.minlink = reader.GetInt32(7);
                site.maxlink = reader.GetInt32(8);

            }

            return site;
        }

        public List<Website> getWebsiteByDomainName(string domainname)
        {

            List<Website> list = new List<Website>();

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "select id,domainname,exp_group,exp_pcnt,imp_value,belong_group,exp_home_pcnt,minlink,maxlink from web_site where deleteflg='0' and domainname = '" + domainname + "'";

            reader = command.ExecuteReader();

            while (reader.Read())
            {
                Website site = new Website();

                site.id = reader.GetInt32(0);
                site.domainname = reader.GetString(1);
                site.exp_group = reader.GetString(2);
                site.exp_pcnt = reader.GetInt32(3);
                site.imp_value = reader.GetInt32(4);
                site.belong_group = reader.GetString(5);
                site.exp_home_pcnt = reader.GetInt32(6);
                site.minlink = reader.GetInt32(7);
                site.maxlink = reader.GetInt32(8);

                list.Add(site);
            }

            return list;
        }

        public List<Website> getWebsiteByBelongGroup(string groupid)
        {

            List<Website> list = new List<Website>();

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "select id,domainname,exp_group,exp_pcnt,imp_value,belong_group,exp_home_pcnt,minlink,maxlink from web_site where deleteflg='0' and belong_group = '" + groupid + "'";

            reader = command.ExecuteReader();

            while (reader.Read())
            {
                Website site = new Website();

                site.id = reader.GetInt32(0);
                site.domainname = reader.GetString(1);
                site.exp_group = reader.GetString(2);
                site.exp_pcnt = reader.GetInt32(3);
                site.imp_value = reader.GetInt32(4);
                site.belong_group = reader.GetString(5);
                site.exp_home_pcnt = reader.GetInt32(6);
                site.minlink = reader.GetInt32(7);
                site.maxlink = reader.GetInt32(8);

                list.Add(site);
            }

            return list;
        }

        public int getTotalImpValue(string groupid)
        {

            //List<Website> list = new List<Website>();
            int totalImpValue = 0;

            MySqlCommand command = conn.CreateCommand();
            MySqlDataReader reader;

            command.CommandText = "select sum(imp_value) from web_site where deleteflg='0' and belong_group = '" + groupid + "'";

            reader = command.ExecuteReader();

            while (reader.Read())
            {
                totalImpValue = reader.GetInt32(0);
            }

            return totalImpValue;
        }
    }
}
